<?php
namespace My\Controller;

use Utuo\BackgroundController;

class ActivityController extends BackgroundController {
	
	private $status = array(
		'0' => '关闭',
		'1' => '开启'
	);
	
	private $prizeStatus = array(
		'0' => '未派奖',
		'1' => '已派奖',
	);
	
	private $model = null;
	
	public function _initialize() {
		$this->model = I("model", "");
		$this->setControllerTitle("活动管理");
		$this->setActionTitle(array(
				'index' => '活动列表',
				'add' => "活动添加",
				'modify' => '活动修改',
				'wechat' => '微信回复设置',
				'userList' => '中奖用户',
				'recycle' => '回收站'
		));
		$this->setActionNav("index,add,recycle");
	}
	
	// 活动类型列表
	public function indexAction() {
		$this->getActivityList(0);
	}
	
	// 回收站
	public function recycleAction() {
		$this->getActivityList(1);
	}
	
	
	// 活动添加
	public function addAction() {
		$activity = array();
		$activity['statusRadio'] = $this->getRadio("status", $this->status, 1);
		$activity['begin_time'] = time();
		$activity['end_time'] = strtotime("+3 day");
		$activity['classOptions'] = $this->getOption(M('ActivityClass')->where('status=1 AND is_deleted=0')->getField("id,name"), "", "==请选择活动类型==");
		$this->assign("prizeConfigJSON", json_encode(array()));
		$this->assign("activity", $activity);
		$this->display("save");
	}
	
	// 活动修改
	public function modifyAction() {
		$activity = $this->getOneById("Activity");
		$activity['statusRadio'] = $this->getRadio("status", $this->status, $activity['status']);
		$activity['prize_config'] = unserialize($activity['prize_config']);
		$activity['classOptions'] = $this->getOption(M('ActivityClass')->where('status=1 AND is_deleted=0')->getField("id,name"), $activity['class_id'], "==请选择活动类型==");
		$this->assign("prizeConfigJSON", json_encode($activity['prize_config'] ));
		$this->assign("activity", $activity);
		$this->display("save");
	}
	
	// 活动保存
	public function savePostAction() {
		$d = D("Activity");
		if( $result = $d->create() and $result = $d->checkPrizeConfig() ) {
			if(!empty($d->id)) {
				$result = $d->save();
			}else {
				$d->cid = $this->aid;
				$result = $d->add();
			}
		}
		$result !== false and $this->ajaxSuccess("保存成功");
		$this->ajaxError($d->getError());
	}
	
	// 微信回复设置
	public function wechatAction() {
		$id = I("id", 0, "intval");
		$activity = M("Activity this0")->join("__ACTIVITY_CLASS__ this1 on this0.class_id=this1.id", "LEFT")->where('this0.id=%d', $id)->field("this0.*,this1.name as className,this1.tag as classTag")->find();
		$wechat_rule = $activity['name'];
		$d = D('WechatReply'); 
		$reply = $d->getReply($this->aid, $wechat_rule);
		if (!$reply) {
			$url = url('Home', 'Activity', 'show', 'sid=' . $this->aid.'&id='.$id, true);
			$id = $d->setKeywordReply($this->aid, $wechat_rule, $activity['className'], $activity["name"], $activity["intro"], $activity["pic"], $url);
			$id && $reply = $d->find($id);
		}
		$reply['keyword'] = trim($reply['keyword'], ' ,');
		$this->assign('reply', $reply);
		$this->display();
	}
	
	// 提交微信回复
	public function wechatPostAction() {
		$d = D('WechatReply');
		if ($d->create()) {
			$d->keyword = $d->parseKeyword($d->keyword);
			if (false !== $d->save()) {
				$this->ajaxSuccess();
			}
		}
		$this->ajaxError();
	}
	
	// 中奖用户列表
	public function userListAction() {
		$id = I("id", 0, "intval");
		$activityRecordLists = M("ActivityRecord a")->join("__USER__ b on a.uid=b.id")->field("b.id,b.name,b.nick_name,b.email,a.id as ac_record_id,a.ac_id,a.status,a.prize")->where("a.ac_id=%d", $id)->select();
		foreach ($activityRecordLists as &$v) {
			$v['statusOptions'] = $this->getOption($this->prizeStatus, $v['status']);
		}
		$this->assign("activityRecordLists", $activityRecordLists);
		$this->display();
	}
	
	public function setAcRecordStatusAction() {
		$acId = I("ac_record_id", 0, "intval");
		$status = I("status", 0, "intval");
		false !== M("ActivityRecord")->where("id=%d and cid=%d", $acId, $this->aid)->setField("status", $status) and $this->ajaxSuccess("设置成功！");
		$this->ajaxError("设置失败！");
	}	
	
	// 设置活动状态
	public function setStatusPostAction() {
		$this->ajaxSetStatus("Activity");
	}
	
	// 移除活动
	public function removePostAction() {
		$this->ajaxRemove ( "Activity" );
	}
	
	// 恢复活动
	public function restorePostAction() {
		$this->ajaxRestore ( "Activity" );
	}
	
	// 彻底删除活动
	public function deletePostAction() {
		$this->ajaxDelete( "Activity" );
	}
	
	// 获取列表
	private function getActivityList($isDeleted) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$pid = I('pid', 0, 'intval');
		$activityLists = M('Activity this0')->join("__ACTIVITY_CLASS__ this1 on this0.class_id=this1.id", "LEFT")->where("this0.cid=%d and this0.is_deleted=%d", $this->aid, $isDeleted)->field("this0.*,this1.name as className")->limit($pagesize)->page($page)->select();
		$activityCount = M('Activity')->where("cid=%d and is_deleted=%d", $this->aid, $isDeleted)->count();
		$this->assign('activityLists', $activityLists);
		$this->showPage($activityCount, $pagesize);
		$this->display();
	}
	
}